﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nvigorate.Extensions;
using Nvigorate.Relational.Provider;

namespace Nvigorate.Test.Relational.Provider
{
    /// <summary>
    /// Summary description for StatefulListTests
    /// </summary>
    [TestClass]
    public class StatefulListTests
    {
        public StatefulListTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void Test_Conversion_Assertion()
        {
            var statefulList = new StatefulList<string>()
                           {
                               "Test1",
                               "Test2",
                               "Test3"
                           };

            IList<string> list = statefulList;

            int index = 0;
            statefulList.ForEach(i => Assert.IsTrue(list[index++].Equals(i)));

            Assert.IsTrue(statefulList.InsertedSet.Count == 3);
            Assert.IsTrue(statefulList.DeletedSet.Count == 0);
        }

        [TestMethod]
        public void Test_Reset_Method()
        {
            var statefulList = new StatefulList<string>()
                           {
                               "Test1",
                               "Test2",
                               "Test3"
                           };
            IList<string> list = statefulList;

            Assert.IsTrue(statefulList.InsertedSet.Count == 3);

            list.Clear();

            Assert.IsTrue(statefulList.DeletedSet.Count == 3);
            Assert.IsTrue(statefulList.InsertedSet.Count == 0);

            statefulList.ResetState();

            Assert.IsTrue(statefulList.DeletedSet.Count == 0);
            Assert.IsTrue(statefulList.InsertedSet.Count == 0);
        }

        [TestMethod]
        public void Test_Add_Item()
        {
            var statefulList = new StatefulList<string>();
            IList<string> list = statefulList;

            list.Add("new value");

            Assert.AreEqual(statefulList.InsertedSet[0], "new value");
            Assert.IsTrue(statefulList.InsertedSet.Count == 1);
        }

        [TestMethod]
        public void Test_Remove_Item()
        {
            var statefulList = new StatefulList<string>()
                           {
                               "Test1",
                               "Test2",
                               "Test3"
                           };

            IList<string> list = statefulList;

            list.RemoveAt(1);
            Assert.IsTrue(statefulList.InsertedSet.Count == 2);
            Assert.IsTrue(statefulList.DeletedSet.Count == 1);
            Assert.AreEqual(statefulList.DeletedSet[0], "Test2");

            list.Remove("Test3");
            Assert.IsTrue(statefulList.InsertedSet.Count == 1);
            Assert.IsTrue(statefulList.DeletedSet.Count == 2);
            Assert.AreEqual(statefulList.DeletedSet[1], "Test3");
        }

        [TestMethod]
        public void Test_List_Manipulation()
        {
            var statefulList = new StatefulList<string>();

            ManipulateList(statefulList);

            Assert.IsTrue(statefulList.Count == 6);
            Assert.IsTrue(statefulList.InsertedSet.Count == 6);
            Assert.IsTrue(statefulList.DeletedSet.Count == 3);

            Assert.AreEqual(statefulList.DeletedSet[0], "B");
            Assert.AreEqual(statefulList.DeletedSet[1], "A");
            Assert.AreEqual(statefulList.DeletedSet[2], "D");
        }


        public void ManipulateList(IList<string> list)
        {
            list.Add("A");
            list.Add("B");
            list.Add("C");
            list.Add("D"); 
            list.Add("E");
            list.Add("F");
            list.Add("G");
            list.Add("H");
            list.Add("I");

            list.Remove("B");
            list.Remove("A");
            list.Remove("D");
        }

    }
}
